package com.qifan.alb2b.action.front;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.qifan.alb2b.action.back.param.UserQueryParam;
import com.qifan.alb2b.action.common.PageController;
import com.qifan.alb2b.domain.NewsInfoDomain;

import com.qifan.alb2b.service.NewsInfoService;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;


@Controller
@RequestMapping("/frontNews")
public class FrontNewsController extends PageController{
	private NewsInfoService newsInfoService;
	

	
	
	public void setNewsInfoService(NewsInfoService newsInfoService) {
		this.newsInfoService = newsInfoService;
	}



	@RequestMapping("/query.usr")
	@ResponseBody
	public Page query(DataGridModel dgm,UserQueryParam uqp) {
		//spring太给力了，可以自动装配两个对象  会自动的装返回的Map转换成JSON对象
		return newsInfoService.query(dgm,new NewsInfoDomain());
	}
	
	
	
	
	/**
	 * 根据type获取信息
	 * @param request
	 * @param type
	 * @return
	 */
	@RequestMapping("/getNews.usr")
	public String getNews(HttpServletRequest request,DataGridModel dgm,NewsInfoDomain rqp,String type){
		
		super.page(request, dgm);//调用父类的分页逻辑
		rqp.setStatus(Constants.NOMARL);//只返回发布的线路
		rqp.setNewsType(type);
		Page page= newsInfoService.query(dgm, rqp);
		request.setAttribute("page", page);
		request.setAttribute("rqp", rqp);

		return "pages/front/news/news";
	}
	
	/**
	 * 根据type获取信息
	 * @param request
	 * @param type
	 * @return
	 */
	@RequestMapping("/queryNews.usr")
	public String queryNews(HttpServletRequest request,DataGridModel dgm,NewsInfoDomain rqp,String newsContent){
		super.page(request, dgm);//调用父类的分页逻辑
		rqp.setStatus(Constants.NOMARL);//只返回发布的线路
		rqp.setNewsContent(newsContent);
		Page page= newsInfoService.query(dgm, rqp);
		request.setAttribute("page", page);
		request.setAttribute("rqp", rqp);
		
//		request.setAttribute("news",newsInfoService.queryNews());
		return "pages/front/news/news";
	}
	
	/**
	 * 根据ID获取信息
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/getNewsById.usr")
	public String getNewsById(HttpServletRequest request, String newsId){
		
		List<NewsInfoDomain> newsInfo=newsInfoService.getNewsById(newsId);
		request.setAttribute("news",newsInfo);
		if(newsInfo.size()>0){
			NewsInfoDomain news = newsInfo.get(0);
			news.setClickCount(newsInfo.get(0).getClickCount()+1);
			newsInfoService.update(news);
		}
		return "pages/front/news/newsDetail";
	}
	
	/**
	 * 获取公司新闻
	 * @param request
	 * @param id(999)
	 * @return
	 */
	@RequestMapping("/queryCompinfo.usr")
	public String queryCompNews(HttpServletRequest request){
		
	

	//	request.setAttribute("compInfo",newsInfoService.getCompNews(Constants.COMP_NEWS_ID));
		return "pages/front/news/compInfo";
	}
	
}
